<?php

namespace app\music\controller;

use app\Base;
use app\model\Common;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

class Index extends Base
{

    /**
     * @return View
     */
    public function show(): View
    {
        return view('index1');
//        return view('index');
    }

    /**
     * 随机获取一条数据
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMusicInfo(): Json
    {
        $id = $this->params['id'] ?? 0;
        $cid = $this->params['current_id'] ?? 0;
        if ((int)$id === 0) {
            $res = Common::getColumn(MusicTable, ['status' => 1], ['id']);
            if ($cid) {
                foreach ($res as $k => $v) {
                    if ($v === (int)$cid) {
                        unset($res[$k]);
                    }
                }
            }
            $id = $res[array_rand($res)];
        }
        if ($id) {
            $res = Common::getData(MusicTable, ['id' => $id], ['id', 'title', 'author', 'pic', 'url']);
            if ($res) {
                $res['url'] = imagePath($res['url']);
                $res['pic'] = imagePath($res['pic']);
            }
        }
        if (!$id || !$res) {
            return $this->returnData('fail', '无播放文件');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    public function searchMusic()
    {
        $name = $this->params['name'] ?? '';
        if (!$name || strlen($name) < 1) {
            return $this->returnData('fail', '无效的歌曲名称');
        }
        $page = $this->params['page'] ?? 1;
        $size = 10;
        $data = [
            'showtype' => 14,
            'highlight' => 'em',
            'pagesize' => 1,
            'tag_aggr' => 1,
            'tagtype' => '全部',
            'keyword' => $name,
            'correct' => 1,
            'api_ver' => 1,
            'version' => 9108,
            'page' => $page,
        ];
        $url = 'http://msearchcdn.kugou.com/api/v3/search/song?' . http_build_query($data);

        $data1 = [
            'version' => 9108,
            'highlight' => 1,
            'keyword' => $name,
            'plat' => 0,
            'pagesize' => 1,
            'area_code' => 1,
            'page' => 1,
            'with_res_tag' => 1,
        ];

        $url1 = 'http://mobileservice.kugou.com/api/v3/lyric/search?' . http_build_query($data1);
//        $data = file_get_contents($url1);
//
//        $startStr = '<!--KG_TAG_RES_START-->';
//        $endStr = '<!--KG_TAG_RES_END-->';
//
//        $data = str_replace($startStr, '', $data);
//        $data = str_replace($endStr, '', $data);
//
//
//        dd(json_decode($data, true));

        $data2 = [
            's' => $name,  //歌曲名
            'offset' => 1, //偏移量
            'limit' => 1,  //获取歌曲数
            'type' => 1,  // 类型(歌曲：1、专辑：10、歌手：100、歌单：1000、用户：1002、mv：1004)
        ];

        $url = 'http://music.163.com/api/search/pc';


        $data = curl($url, $data2);
        dd($data);


//      $url = 'http://mobileservice.kugou.com/api/v3/lyric/search?version=9108&highlight=1&keyword=%E7%AE%80%E7%AE%80%E5%8D%95%E5%8D%95-%E6%9E%97%E4%BF%8A%E6%9D%B0&plat=0&pagesize=1&area_code=1&page=1&with_res_tag=1';
    }

}